﻿using System;
using System.Linq;
using LX.Core.Validation;
using S4Store.Domain.CarKpi;
using S4Store.Persistent;
using S4Store.Domain.SellerKpi;
using S4Store.Domain.TelSaleKpi;

namespace S4Store.Service.Valid.TelSaleKpiValid
{
    public class CarBrandTelSaleMonthConfigSave : ValidExpression<CarBrandTelSaleMonthConfig>
    {
        public S4StoreDataContext PersistentContext { get; set; }

        public override void ValidEntity(ValidationResult result, CarBrandTelSaleMonthConfig entity)
        {
            if (entity.Month == DateTime.MinValue)
            {
                PushError(result, "日期不正确");
            }
            if (entity.StoreId == 0)
            {
                PushError(result, "店不正确");
            }

            if (PersistentContext.Set<CarBrandTelSaleMonthConfig>().AsQueryable().Any(t => t.StoreId == entity.StoreId && t.Month.Year == entity.Month.Year && t.Month.Month == entity.Month.Month && t.Id != entity.Id))
            {
                PushError(result, "这个月已经有一条计划记录了");
            }
        }

    }
}
